//
//  UserDetailViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/2/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "UserDetailViewController.h"
#import "HandWorkTableviewCell.h"
#import "UserHandWorkModle.h"
#import "UserCourseModel.h"
#import "UserCourseCollectionViewCell.h"
#import "CourseDetailViewController.h"
@interface UserDetailViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;//scrollview
@property (nonatomic, retain)UITableView *tableview;//tableview
@property (nonatomic, retain)UICollectionView *collectionView;//collectionView
@property (nonatomic, retain)NSMutableArray *dataArr;//tableview数据
@property (nonatomic, retain)NSMutableArray *collectionArr;//collectionView数据
@property (nonatomic, retain)NSMutableArray *btnArr;
@property (nonatomic, retain)NSMutableArray *lineArr;

@property (nonatomic, retain)NSMutableArray *iArr;
@end

@implementation UserDetailViewController
-(void)dealloc
{
    [_scrollView release];
    [_tableview release];
    [_collectionView release];
    [_dataArr release];
    [_collectionArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];//背景颜色
    [self data];//tableviw数据
    [self collectionData];//collectionView 数据
    
    //头部View
    UIImageView *bgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    [bgview sd_setImageWithURL:[NSURL URLWithString:self.faceurl]];
    [self.view addSubview:bgview];
    //高斯模糊
    UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *bv = [[UIVisualEffectView alloc]initWithEffect:b];
    bv.frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
    [bgview addSubview:bv];
    [bv release];
    
    
    
    
    [bgview release];
    
    //头像
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    [img sd_setImageWithURL:[NSURL URLWithString:self.faceurl]];
    //圆角
    img.layer.cornerRadius = 40;
    img.clipsToBounds = YES;
    img.center = bgview.center;//位置
    [bgview addSubview:img];
    [img release];
    
    
    //名字
    
    UILabel *namelabel = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 3, img.frame.size.height + 120, [UIScreen mainScreen].bounds.size.width / 3, 30)];
    namelabel.textAlignment = NSTextAlignmentCenter;//居中
    namelabel.text =  self.namelabel;
    [bgview addSubview:namelabel];
    [namelabel release];
    
    
   
    
    //粉丝  关注
    
    UIButton *fans = [UIButton buttonWithType:UIButtonTypeSystem];
    fans.frame = CGRectMake(self.view.frame.size.width - 100, namelabel.frame.size.height + namelabel.frame.origin.y + 5, 80, 30);
    //字体颜色
    fans.tintColor = [UIColor blackColor];
    //按钮名字
    [fans setTitle:@"粉丝 | 关注" forState:UIControlStateNormal];
    //按钮点击事件
    [fans addTarget:self action:@selector(fans:) forControlEvents:UIControlEventTouchUpInside];
    [bgview addSubview:fans];
    
    
    
    //button
    
    [self creatbut];
    
    
    
    
    #pragma mark - scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 330, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    //scrollView

    [self.view addSubview:self.scrollView];//添加
    self.scrollView.delegate= self;//代理人
    self.scrollView.pagingEnabled = YES;//整页翻动
    self.scrollView.contentSize =  CGSizeMake(self.view.frame.size.width * 2, 0);//实际内容
    [_scrollView release];
    
    
    #pragma mark - tableview
    
   self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 349) style:UITableViewStylePlain];
    //代理
    self.tableview.dataSource = self;
    self.tableview.delegate = self;

    [self.scrollView addSubview:self.tableview];
    [_tableview release];
//重用池
    [_tableview registerClass:[HandWorkTableviewCell class] forCellReuseIdentifier:@"tabcell"];
//
    #pragma mark - collectionView
    //layout布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 2, 200);//item大小
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);//边距
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;//竖向滑动
   
    //创建ColletionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 349) collectionViewLayout:layout];//
    //代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];//背景颜色
    [self.scrollView addSubview:self.collectionView];//把collectionview添加scrollView上
    //    self.materialView
    [_collectionView registerClass:[UserCourseCollectionViewCell class] forCellWithReuseIdentifier:@"collcell"];//collectionView重用池

    [_collectionView release];
    [layout release];

    
    
    
}
- (void)creatbut
{
    NSArray *arr = @[@"手工圈",@"教程" ];
    
    self.btnArr = [NSMutableArray array];
    self.lineArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(i * self.view.frame.size.width / 2, 300, self.view.frame.size.width / 2, 29);
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.tag = 1000 + i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addObject:btn];
        [self.view addSubview:btn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 * i, 329, self.view.frame.size.width / 2, 1)];
        line.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:line];
        line.tag = 2000 + i;
        [self.lineArr addObject:line];
        [line release];
        if (i == 0) {
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            line.backgroundColor = [UIColor redColor];
        }
        
    }
    
}

#pragma mark -   手工圈 和教程 按钮点击方法
- (void)btnAction:(UIButton *)btn
{
    [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width * (btn.tag - 1000), 0) animated:YES];
    for (UIButton *btn in _btnArr) {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    for (UIView *line in _lineArr) {
        line.backgroundColor = [UIColor whiteColor];
    }
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    UIView *line = [self.view viewWithTag:btn.tag + 1000];
    line.backgroundColor = [UIColor redColor];
}
#pragma mark -  scrollView 拖拽方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView == _scrollView){
        NSInteger i= scrollView.contentOffset.x / self.view.frame.size.width;
        for (UIButton *btn in _btnArr) {
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [_btnArr[i] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        for (UIView *line in _lineArr) {
            line.backgroundColor = [UIColor whiteColor];
        }
        [_lineArr[i] setBackgroundColor:[UIColor redColor]];
    }

}
#pragma mark - tableview必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HandWorkTableviewCell *tabcell = [tableView dequeueReusableCellWithIdentifier:@"tabcell" forIndexPath:indexPath];
    UserHandWorkModle *user = _dataArr[indexPath.row];
    //头像
    [tabcell.touxiangimg sd_setImageWithURL:[NSURL URLWithString:user.avatar]];
    //名字
    tabcell.name.text = user.uname;
    tabcell.content.text = user.subject;
    UserHandWorkModle *user1 = _iArr[indexPath.row];
    tabcell.usermodel = user1;
    
    return tabcell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserHandWorkModle *user = _dataArr[indexPath.row];
    CGFloat h = [HandWorkTableviewCell heightWithText:user.subject];
    
    
    
    CGFloat imgCVH = 0;
    // 高度自适应
    if (self.iArr.count == 0) {
        imgCVH = 0;
    } else if (self.iArr.count < 4) {
        imgCVH = (self.view.frame.size.width) / 3;
    } else if (self.iArr.count < 7) {
        imgCVH = (self.view.frame.size.width - 10) / 3 * 2;
    } else {
        imgCVH = (self.view.frame.size.width - 20);
    }
    NSLog(@"%ld",self.iArr.count);
    return h + imgCVH + 80;
}
#pragma mark - collectionView必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UserCourseCollectionViewCell *collcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collcell" forIndexPath:indexPath];
    UserCourseModel *userCour = _collectionArr[indexPath.item];
    //头像
    [collcell.bgimg sd_setImageWithURL:[NSURL URLWithString:userCour.host_pic_s]];
    //名字
    collcell.name.text = userCour.subject;
    //人气, 赞
    collcell.other.text = [[[userCour.view stringByAppendingString:@"人气 | "] stringByAppendingString:userCour.laud] stringByAppendingString:@"赞"];
    //时间
    collcell.timelabel.text = userCour.add_time;
    return collcell;
}

#pragma mark - tableview数据
- (void)data
{
    NSString *str = [[@"http://m.shougongke.com/index.php?c=User&a=getCircleList&cate_id=2&uid=" stringByAppendingString:self.uid] stringByAppendingString:@"&vid=15"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        self.dataArr = [UserHandWorkModle modelHandlerWithArray:arr];
        
        self.iArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            NSArray *iArr = [ dic objectForKey:@"pic"];
            UserHandWorkModle *ere =[[ UserHandWorkModle alloc]init];
            ere.arr5 = [NSMutableArray array];
            for (NSDictionary *dicarr in iArr) {
                [ere.arr5 addObject:[dicarr objectForKey:@"url"]];
            }
            [self.iArr addObject:ere];
            
            NSLog(@"%@",ere.arr5);
        }
        [self.tableview reloadData];
    }];
    
    
}
#pragma mark - collectionView数据处理
- (void)collectionData
{
    NSString *str = [[@"http://m.shougongke.com/index.php?&c=User&a=myCourse&uid=" stringByAppendingString:self.uid] stringByAppendingString:@"&vid=15"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        self.collectionArr = [UserCourseModel modelHandlerWithArray:arr];
        [_collectionView reloadData];
    }];
}

#pragma mark - 粉丝按钮点击方法
- (void)fans:(UIButton *)fan
{
    NSLog(@"粉丝");
}
#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CourseDetailViewController *cour = [[CourseDetailViewController alloc]init];
    UserCourseModel *user = _collectionArr[indexPath.item];
    cour.deid = user.hand_id;
    [self.navigationController pushViewController:cour animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
